<?php

//------------------------
// 类型属性控制器
//-------------------------
namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\TypeAttr as TypeAttrModel;
use app\common\model\Goods as GoodsModel;
use app\common\model\GoodsTypeAttrView as GoodsTypeAttrViewModel;
use think\Config;
use think\Db;

class TypeAttr extends AdminBase
{
    protected $TypeAttrModel;
    protected $GoodsModel;
    protected $GoodsTypeAttrViewModel;

    protected function _initialize()
    {
        parent::_initialize();
        $this->TypeAttrModel               = new TypeAttrModel();
        $this->GoodsModel                  = new GoodsModel();
        $this->GoodsTypeAttrViewModel      = new GoodsTypeAttrViewModel();
    }


    // 属性列表
    public function index(){
        $id = $this->request->param('id',0,'intval');

        $where['type_id'] = array('eq',$id);

        $info = $this->TypeAttrModel->where($where)->order('seq')->paginate(10);

        $this->assign('type_id',$id);
        $this->assign('info',$info);

        return $this->fetch('index');
    }


    // 添加页面
    function add(){
        $type_id = $this->request->param('type_id',0,'intval');

        if (!$type_id) {
            $this->error('缺少添加属性的类型type_id');
        }

        $this->assign('type_id',$type_id);
        return $this->fetch('add');
    }

    /**
     * @desc 添加操作
     */
    public function addProc() {
        $data            = $this->request->param();

        $validate_result = $this->validate($data, 'TypeAttr.add');

        if ($validate_result !== true) {
                $this->error($validate_result);
        }

        $res = $this->TypeAttrModel->save($data);

        if ($res === false) {
            $this->error($this->TypeAttrModel->getError());
        } else {
            $this->success('添加成功');
        }
    }

    
    // 编辑页面
    function edit(){
        $id = $this->request->param('id',0,'intval');

        $data = $this->TypeAttrModel->get($id);

        $this->assign('data',$data);
        return $this->fetch('edit');
    }

    // 编辑操作
    function editProc(){
        $data  = $this->request->param('');

        $validate_result = $this->validate($data, 'TypeAttr.edit');

        if ($validate_result !== true) {
                $this->error($validate_result);
        }

        $res = $this->TypeAttrModel->allowField(true)->save($data,$data['id']);

        if ($res === false) {
            $this->error($this->TypeAttrModel->getError());
        } else {
            $this->success('保存成功');
        }
    }



    // 删除操作
    public function delete(){
        $id = $this->request->param('id',0,'intval');

        $where['id'] = array('eq',$id);

        $res = $this->TypeAttrModel->where($where)->delete();

        if ($res == false) {
            $this->error($this->TypeAttrModel->getError());
        } else{
            // 删除成功后，才删除中间表的数据
            $this->success('删除成功');
        }
    }

    // 根据传入的类型id，ajax获取对应的属性
    public function ajaxGetAttr(){
        $id          = $this->request->param('id',0,'intval');
        $goods_id    = $this->request->param('goods_id',0,'intval');

        $goods_type_id =  Db('Goods')->where('id',$goods_id)->field('type_id')->find();

        if ($id == $goods_type_id['type_id']) {
            // 如果选择的是原来的商品类型

            $goodsAttrViewInfo = $this->GoodsTypeAttrViewModel->where('platform_goods_attr.goods_id',$goods_id)->select()->toArray();

            // 找到当前商品的所有属性id
            $attr_id = array(0);
            foreach ($goodsAttrViewInfo as $k => $v){
                $attr_id[] = $v['attr_id'];
            }
            $attr_id = array_unique($attr_id);//去掉重复

           // 取出当前类型下的后添加的新属性
            $otherAttrInfo = $this->TypeAttrModel->where('type_id',$goods_type_id['type_id'])->where('id','not in',$attr_id)->field('attr_name,is_select,opts,id AS attr_id')->select()->toArray();

            //为属性值，属性价格，商品属性id都添加一个空值
            foreach ($otherAttrInfo as $k => $v) {
                $otherAttrInfo[$k]['attr_value'] = '';        
                $otherAttrInfo[$k]['attr_price'] = '';
                $otherAttrInfo[$k]['goods_attr_id'] = '';
            }

            // 把新的属性和原属性合并起来
            if ($otherAttrInfo) {
                $goodsAttrViewInfo = array_merge($goodsAttrViewInfo, $otherAttrInfo);
                usort($goodsAttrViewInfo, 'attr_id_sort');
            }

            // halt($goodsAttrViewInfo);
            $data = $goodsAttrViewInfo;
            $info = "";
            $status = 2;

        } else {

            //如果选择其他商品类型
            $where['type_id'] = array('eq',$id);

            $data = $this->TypeAttrModel->where($where)->select()->toArray();

            // $this->success($data);
            $info = '';
            $status = 1;

        }

         $ajaxData = array(
                        'info'   => $info,
                        'status' => $status,
                        'data'   => $data
                    );

        return $ajaxData;
    }

    // 根据传入的类型id，获取对应检索的属性
    public function ajaxGetSearchAttr(){
        $type_id          = $this->request->param('type_id',0,'intval');

        $data = $this->TypeAttrModel->field('id,attr_name')->where('type_id',$type_id)->where('search_index',1)->select()->toArray();

        if ($data) {
            $info = '';
            $status = 1;
        } else {
            $info = '';
            $status = 0;
        }

        $ajaxData = array(
                        'info'   => $info,
                        'status' => $status,
                        'data'   => $data
                    );

        return $ajaxData;
    }




}